<template>
	<view>
		<!-- <view class="head">
			<view class="size">已上传文件(已使用空间12.5M/500M)</view>
			<view class="history">打印记录</view>
		</view> -->
		<view class="file_list">
			<checkbox-group v-if="fileList.length > 0" name="">
				<view class="file_item" v-for="(item,index) in fileList" :key="item.id">
					<view class="icon">
						<image src="/static/images/print/word.png" mode="scaleToFill"></image>
					</view>
					<view class="file_msg">
						<view class="file_name">{{item.name}}</view>
						<view class="time_size">{{(item.size/1024).toFixed(2)}} KB</view>
					</view>
					<view class="file_delbtn" @click="delFile(index)">
						<image src="../../../static/images/print/delect.png" mode="scaleToFill"></image>
					</view>
				</view>
			</checkbox-group>
			<view class="file_item add_item" @click="getFile">
				<view class="icon">
					<image src="/static/images/print/add.png" mode="scaleToFill"></image>
				</view>
				<view class="file_msg">
					<view class="file_name">添加上传文件</view>
				</view>
			</view>
		</view>
		<view class="btn_box">
			<button type="default" class="btn_pri" @click="uploadHandle">上传文件</button>
		</view>
	</view>
</template>

<script>
	import {
		uploadPrintFile,
		queryStudentCardBalance
	} from "../../../common/api/index.js";
	import {
		imgURL,
		requestURL
	} from '../../../common/utils/config.js'
	import {
		_showLoading,
		_showToast
	} from '../../../common/js/util.js'
	let _this;
	export default {
		data() {
			return {
				fileList: [],
				geListStu: []
			}
		},
		mounted() {
			_this = this
			_this._queryStudentCardBalance()
		},
		methods: {
			getFile() {
				// #ifdef MP-WEIXIN
				// let fileSystem = wx.getFileSystemManager()
				// console.log(fileSystem.readFile())
				wx.chooseMessageFile({
					count: 10,
					type: 'file',
					extension: ['docx', 'doc'],
					success(res) {
						// tempFilePath可以作为img标签的src属性显示图片
						console.log(res)
						res.tempFiles.forEach(item => {
							_this.fileList.push(item)
						})
					}
				})
				// #endif
			},
			delFile(index) {
				_this.fileList.splice(index, 1);
			},
			_queryStudentCardBalance() {
				let s_userId = uni.getStorageSync("s_userId");
				queryStudentCardBalance({
						userId: s_userId,
						queryCard: true,
						queryWallet: true,
						bindType: 0
					}).then(res => {
						if (res.code == 0) {
							_this.geListStu = res.data.students;
						} else {
							_this.geListStu = [];
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			uploadHandle() {
				let len = _this.fileList.length;
				if (len <= 0) {
					_showToast('添加需要上传的文件');
					return
				}
				_showLoading('上传中,请稍后...')
				_this._uploadFile(len - 1)
			},
			_uploadFile(index) {
				if (index < 0) {
					_showToast('上传成功')
					uni.navigateBack({
						delta: 1
					})
					return;
				}
				uni.uploadFile({
					url: requestURL + '/print/info/uploadPrintFile',
					filePath: _this.fileList[index].path,
					name: 'file', // 文件对应的key ，默认 为 file
					formData: {
						'empId': _this.geListStu[0].students[0].empId,
						'fileName': _this.fileList[index].name
					},
					header: {
						"Content-Type": "multipart/form-data"
					},
					success(uploadFileRes) {
						const res = JSON.parse(uploadFileRes.data)
						if (res.code == 0) {
							_this._uploadFile(index - 1)
						} else {
							uni.hideLoading()
							_showToast('上传失败')
						}
					},
					fail(err) {
						uni.hideLoading()
						_showToast(err.errMsg)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.head {
		padding: 0 20rpx;
		height: 70rpx;
		box-shadow: 0 0 20px 1px #e8e8e8;
		font-size: 24rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.file_list {
		height: 100vh;
		padding-bottom: 160rpx;
		overflow: auto;
		box-sizing: border-box;

		.file_item {
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;
			margin: 20rpx 10rpx;

			&.add_item {
				border: 1px dashed #bfbfbf;
				border-radius: 6rpx;
				color: #999;
			}

			.icon,
			.file_delbtn {
				width: 100rpx;
				height: 100rpx;
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
			}

			.file_delbtn {
				image {
					width: 48rpx;
					height: 48rpx;
				}
			}

			.icon {
				image {
					width: 82rpx;
					height: 82rpx;
				}
			}

			.file_msg {
				width: calc(100vw - 200rpx);

				.file_name,
				.time_size {
					padding: 6rpx;
				}

				.time_size {
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}

	.btn_box {
		position: fixed;
		width: 100vw;
		bottom: 36rpx;

		.btn_pri {
			margin-top: 60rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			width: 90%;
			font-size: 32rpx;
			font-family: "PingFangSC-Heavy, PingFang SC";
			font-weight: 600;
			color: #ffffff;
			background: linear-gradient(90deg, #0685ff 0%, #285eff 100%);
			box-shadow: 0px 8px 16px 0px rgba(36, 98, 255, 0.24);
			border-radius: 8rpx;
		}
	}
</style>
